package cn.lanqiao.view;

import cn.lanqiao.entity.Student;
import cn.lanqiao.service.StudentService;
import cn.lanqiao.service.serviceImpl.StudentServiceImpl;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentView {
    static StudentService ss = (StudentService) new StudentServiceImpl();
    static Scanner sc = new Scanner(System.in);

    public void StudentMainView() {
        while (true) {
            System.out.println("-------------------------------");
            System.out.println("1.增加学生");
            System.out.println("2.查询学生");
            System.out.println("3.修改学生");
            System.out.println("4.删除学生");
            System.out.println("5.分页查询");
            System.out.println("6.按照序号排序");
            System.out.println("7.统计学生");
            System.out.println("8.批量操作");
            System.out.println("9.返回上一级");
            System.out.println("请选择:");
            String choose = sc.next();
            if (choose.equals("1")) {
                addStudent();
            } else if (choose.equals("2")) {
                queryStudent();
            } else if (choose.equals("3")) {
                updateStudent();
            } else if (choose.equals("4")) {
                deleteStudent();
            } else if (choose.equals("5")) {
                sortStudent();
            } else if (choose.equals("6")) {
                orderStudent();
            } else if (choose.equals("7")) {
                countStudent();
            } else if (choose.equals("8")) {
                batchStudent();
            } else if (choose.equals("9")) {
                break;
            } else {
                System.out.println("请按要求输入！");
            }
        }
    }




    //增加学生
    private static void addStudent() {
        System.out.println("--------------------------");
        System.out.println("请输入学生学号：");
        int stu_id = sc.nextInt();
        System.out.println("请输入学生名字：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生性别：");
        String gender = sc.next();
        //创建学生对象
        Student s = new Student(stu_id, name, age, gender);
        //调用service存入
        int i = ss.addStudent(s);
        if (i > 0) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！稍后重试");
        }
    }

    //查询学生
    private static void queryStudent() {
        while (true) {
            System.out.println("-------------------------------");
            System.out.println("1.查询所有学生");
            System.out.println("2.按学号查询");
            System.out.println("3.返回上一级");
            System.out.println("请选择:");
            String choose = sc.next();
            if (choose.equals("1")) {
                ArrayList<Student> stus = queryAllStudent();
                if (stus == null || stus.size() == 0) {
                    System.out.println("系统中没有任何学生");
                } else {
                    showAllStudent(stus);
                }
            } else if (choose.equals("2")) {
                //按id查询班级 设计多表查询
                Student student = queryStudentById();
                if (student == null) {
                    System.out.println("系统中没有该学生");
                } else {
                    showStudent(student);
                }
            } else if (choose.equals("3")) {
                break;
            } else {
                System.out.println("请按要求输入！");
            }
        }
    }

    private static ArrayList<Student> queryAllStudent() {
        ArrayList<Student> stus = ss.queryAllStudent();
        return stus;
    }

    private static void showAllStudent(ArrayList<Student> stus) {
        for (int i = 0; i < stus.size(); i++) {
            Student s = stus.get(i);
            System.out.println("-------------------------------");
            System.out.println("学号：" + s.getStudent_id());
            System.out.println("学生名字:" + s.getName());
            System.out.println("学生年龄:" + s.getAge());
            System.out.println("学生性别:" + s.getGender());
            if (i < stus.size() - 1) {
                //利用键盘输入阻塞运行等待输入
                System.out.println("输入1输出下一个学生信息");
                sc.next();
            }
        }
    }

    //按照学号查找学生
    private static Student queryStudentById() {
        System.out.println("-------------------------------");
        System.out.println("请输入学号");
        String student_id = sc.next();
        return ss.queryStudentById(Integer.parseInt(student_id));
    }

    private static void showStudent(Student student) {
        System.out.println("---------------以下为该学生信息-------------");
        System.out.println("学号:" + student.getStudent_id());
        System.out.println("学生名字:" + student.getName());
        System.out.println("学生年龄:" + student.getAge());
        System.out.println("学生性别:" + student.getGender());
    }

    //修改学生
    private void updateStudent() {
        //查询id的方法查出要修改的学生
        Student student = queryStudentById();
        //打印要修改的学生基本信息
        System.out.println("您要修改的学生信息如下");
        System.out.println("学号：" + student.getStudent_id());
        System.out.println("学生名字：" + student.getName());
        System.out.println("学生年龄：" + student.getAge());
        System.out.println("学生性别：" + student.getGender());
        // 并询问是否要修改基本信息
        System.out.println("是否修改信息？（1.是，2.否）");
        String choose = sc.next();
        if (choose.equals("1")) {
            updateStudentInfo(student);
        }
    }

    private void updateStudentInfo(Student student) {
        System.out.println("请输入学号");
        int student_id = sc.nextInt();
        System.out.println("请输入学生名字");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生性别");
        String gender = sc.next();
        //修改学生信息
        student.setName(name);
        student.setStudent_id(student_id);
        student.setGender(gender);
        student.setAge(age);
        //调用service修改
        int i = ss.updateStudentInfo(student);
        if (i > 0) {
            System.out.println("修改成功！");
        }
    }

    //删除学生
    private void deleteStudent() {
        // 调用查询单个学生的方法查出要删除的班级信息
        Student student = queryStudentById();
        // 打印要修改的班级基本信息
        System.out.println("您要修改的学生信息如下");
        System.out.println("学号：" + student.getStudent_id());
        System.out.println("学生名字：" + student.getName());
        System.out.println("学生年龄：" + student.getAge());
        System.out.println("学生性别：" + student.getGender());
        // 并询问是要修改基本信息还是学生还是老师
        System.out.println("是否删除学生？（1.是，2.否）");
        String choose = sc.next();
        if (choose.equals("1")) {
            int i = ss.deleteStudent(student.getStudent_id());
            if (i > 0) {
                System.out.println("删除成功！");
            }
        }
    }

    //分页查询学生
    private void sortStudent() {
        System.out.println("请输入你想要查询的页数:");
        int pageNum = sc.nextInt();
        System.out.println("请输入你要查询的记录数:");
        int pageSize = sc.nextInt();
        ArrayList<Student> student = queryStudentByPage(pageNum, pageSize);
        if (student == null) {
            System.out.println("当前页面没有学生信息");
        } else {
            showSortStudent(student);
        }

    }

    private ArrayList<Student> queryStudentByPage(int pageNum, int pageSize) {
        return ss.queryStudentBypage(pageNum, pageSize);

    }

    private void showSortStudent(ArrayList<Student> student) {
        for (int i = 0; i < student.size(); i++) {
            Student s = student.get(i);
            System.out.println("-------------------------------");
            System.out.println("学号：" + s.getStudent_id());
            System.out.println("学生名字:" + s.getName());
            System.out.println("学生年龄:" + s.getAge());
            System.out.println("学生性别:" + s.getGender());
//            if (i < student.size() - i) {
//                //利用键盘输入阻塞运行等待输入
//                System.out.println("输入1输出下一页学生信息");
//                sc.next();
//            }
        }
    }

    //排序功能
    private void orderStudent() {
        ArrayList<Student> stus = orderAllStudent();
        if (stus == null || stus.size() == 0) {
            System.out.println("系统中没有任何学生");
        } else {
            showOrderStudent(stus);
        }
    }

    private ArrayList<Student> orderAllStudent() {
        ArrayList<Student> stus = ss.queryAllStudent();
        return stus;
    }

    private void showOrderStudent(ArrayList<Student> stus) {
        for (int i = 0; i < stus.size(); i++) {
            Student s = stus.get(i);
            System.out.println("-------------------------------");
            System.out.println("学号：" + s.getStudent_id());
            System.out.println("学生名字:" + s.getName());
            System.out.println("学生年龄:" + s.getAge());
            System.out.println("学生性别:" + s.getGender());
            if (i < stus.size() - 1) {
                //利用键盘输入阻塞运行等待输入
                System.out.println("输入1输出下一个学生信息");
                sc.next();
            }
        }
    }

    //统计功能
    private void countStudent() {
        System.out.println("学生总人数为：" + ss.totalStudents());
        System.out.println("男生人数： " + ss.maleStudents());
        System.out.println("女生人数： " + ss.femaleStudents());
        System.out.printf("男女比例： %.2f%n", ss.maleToFemaleRatio());
    }
    //批量操作
    //批量添加
     private void batchStudent() {
        while (true){
            System.out.println("-------------------------------");
            System.out.println("1.批量添加学生");
            System.out.println("2.批量删除学生");
            System.out.println("3.退出");
            System.out.println("请输入你的选择：");
            String choose = sc.next();
            if (choose.equals("1")){
                batchAddStudent();
                System.out.println("---------------批量添加成功-------------");
            } else if (choose.equals("2")) {
                batchDeleteStudent();
                System.out.println("---------------批量删除成功-------------");
            }else if (choose.equals("3")){
                break;
            }else {
                System.out.println("请按要求输入！");
            }
        }
    }
    //批量删除
    private void batchAddStudent() {
        System.out.println("请输入要添加的学生数量：");
        int count = sc.nextInt();
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            System.out.println("请输入第 " + (i + 1) + " 个学生的学号：");
            int student_id = sc.nextInt();
            System.out.println("请输入第 " + (i + 1) + " 个学生的姓名：");
            String name = sc.next();
            System.out.println("请输入第 " + (i + 1) + " 个学生的年龄：");
            int age = sc.nextInt();
            System.out.println("请输入第 " + (i + 1) + " 个学生的性别：");
            String gender = sc.next();
            students.add(new Student(student_id,name,age,gender));
        }
        ss.batchAddStudentsService(students);
    }
    private void batchDeleteStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生 ID 数量：");
        int count = scanner.nextInt();
        ArrayList<Integer> student_id = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            System.out.println("请输入第 " + (i + 1) + " 个要删除的学生 ID：");
            int student_ids = scanner.nextInt();
            student_id.add(student_ids);
        }

        ss.batchDeleteStudentsService(student_id);
   }

}


